package com.itheima.controller;

import com.itheima.vo.Result;
import com.itheima.pojo.UmsMenu;
import com.itheima.service.UmsMenuService;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.UmsMenuNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("menu")
public class UmsMenuController {
    @Autowired
    private UmsMenuService umsMenuService;

    /**
     * 添加后台菜单
     * @param umsMenu
     * @return
     */
    @PostMapping("create")
    public Result create(@RequestBody UmsMenu umsMenu){
        umsMenuService.create(umsMenu);
        return Result.success(null);
    }

    /**
     * 根据ID删除后台菜单
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public Result deleteById(@PathVariable("id")Integer id){
        umsMenuService.deleteById(id);
        return Result.success(null);
    }


    /**
     * 分页查询后台菜单
     * @param parentId 父级id,0是一级菜单,1是二级菜单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list/{parentId}")
    public Result findMenuByParentId(@PathVariable("parentId")Integer parentId,
                                     @RequestParam(defaultValue = "1")Integer pageNum,
                                     @RequestParam(defaultValue = "10")Integer pageSize){
        if(pageSize==5){
            pageSize=10;
        }
        PageResultVo result=umsMenuService.findMenuByParentId(parentId,pageNum,pageSize);
        return Result.success(result);
    }

    /**
     * 修改后台菜单
     * @param id
     * @param umsMenu
     * @return
     */
    @PostMapping("update/{id}")
    public Result editById(@PathVariable("id")Integer id,@RequestBody UmsMenu umsMenu){
        umsMenuService.editById(id,umsMenu);
        return Result.success(null);
    }

    /**
     * 修改菜单显示状态
     * @param hidden
     * @param id
     * @return
     */
    @PostMapping("updateHidden/{id}")
    public Result editHidden(Integer hidden,@PathVariable("id") Integer id){
        umsMenuService.editHidden(id,hidden);
        return Result.success(null);
    }

    /**
     * 根据ID获取菜单详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result findOneById(@PathVariable("id") Integer id){
        UmsMenu umsMenu=umsMenuService.findOneById(id);
        return Result.success(umsMenu);
    }
    /**
     * 树形结构返回所有菜单列表
     * @return
     */
    @GetMapping("treeList")
    public Result treeList(){
        List<UmsMenuNode> children =umsMenuService.treeList();
        return Result.success(children);
    }

}
